jQuery(function($) {

    // on select of team fill the group
    $(document).on('change', 'select[name=teamId]', function() {
        $this = $(this);
        var teamId = $this.val();
        $( '#members' ).html( '' ).parent().hide();
        $( '#spendBy' ).html( '' ).parent().hide();
        if( teamId == 0 || teamId == '' ){
            return false;
        }
        $.ajax({
            url: '/ajax/admin/',
            type: 'post',
            data: { method : 'getTeamMembers', id : teamId },
            success: function(resp) {
                // check save action
                eval('var respObj = ' + resp);
                if (respObj.status) {
                    alert(respObj.msg);
                    return false;
                } else {
                    eval('var respObj = ' + resp);
                    var members = respObj.members;
                    var newMembersHtml = '';
                    var newMembersHtml1 = '';
                    for (var n in members) {
                        newMembersHtml += '<label><input name="members[' + members[n].id + ']" type="checkbox" value="' + members[n].id + '" /> ' + members[n].text + '</label><br />';
                        newMembersHtml1 += '<label><input name="spendBy" type="radio" value="' + members[n].id + '" /> ' + members[n].text + '</label><br />';
                    }
                    $( '#members' ).html( newMembersHtml ).parent().show();
                    $( '#spendBy' ).html( newMembersHtml1 ).parent().show();
                }
            }
        });
    });

    $( 'button[name=addExpenseBtn]' ).on( 'click', function(){
        $( '#formMsg' ).html( '' );
        var teamId = $.trim( $( 'select[name=teamId]' ).val() );
        if( teamId == 0 || teamId == '' ){
            $( 'select[name=teamId]' ).focus();
            $( '#formMsg' ).html( 'Select team first !' );
            return false;
        }

        var members = $.trim( $( 'input[type=checkbox]:checked' ).length );
        if( members == 0 || members == '' ){
            $( 'input[type=checkbox]' ).focus();
            $( '#formMsg' ).html( 'Choose the members first !' );
            return false;
        }

        var spendBy = $.trim( $( 'input[type=radio]:checked' ).length );
        if( spendBy == 0 || spendBy == '' ){
            $( 'input[type=radio]' ).focus();
            $( '#formMsg' ).html( 'Choose who spend money !' );
            return false;
        }

        var description = $.trim( $( 'textarea[name=description]' ).val() );
        if( description == 0 || description == '' ){
            $( 'textarea[name=description]' ).focus();
            $( '#formMsg' ).html( 'Enter expense detail !' );
            return false;
        }

        var money = $.trim( $( 'input[name=money]' ).val() );
        if( money == '' ){
            $( 'input[name=money]' ).focus();
            $( '#formMsg' ).html( 'Enter spend amount !' );
            return false;
        }

        if( isNaN( money ) ){
            $( 'input[name=money]' ).focus().val( '' );
            $( '#formMsg' ).html( 'Enter valid amount !' );
            return false;
        }

    });

});